package com.twl.app.base.bo;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.twl.app.base.entity.CheckDate;
import com.twl.app.base.entity.Checktemplet;
import com.twl.app.base.entity.Jbsalary;
import com.twl.app.base.entity.LockState;
import com.twl.app.base.entity.OpenState;
import com.twl.app.base.entity.Personalmoney;
import com.twl.app.base.entity.SalSalaryData;
import com.twl.app.base.entity.Uemp;
import com.twl.app.base.util.CryptUtils;
import com.twl.common.constant.SalConstant;
import com.twl.core.framework.bo.impl.AbsCrudBoImpl;
import com.twl.core.framework.dao.WSHibernateDaoSupportProxy;
@Transactional
@Repository("commonBo")
public class CommonBoImpl extends AbsCrudBoImpl {
	
	@Autowired
	private WSHibernateDaoSupportProxy wSHibernateDaoSupportProxy;
	
	/**
	 * 查询uemp list 用于转map
	 * @param staffint
	 * @return
	 */
	public List<Uemp> getUemps(){
		List<Uemp> uempList = super.findAll(" from Uemp where  delstaff=0");
		List<Uemp> list =null;
	
		try {
			//解密
			 list = CryptUtils.decryptNew(uempList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(list!=null){
			return list ;
		}
		return null;
	}
	
	public String updateUemp(Uemp uemp){//修改状态不需要加解密
		try {
			super.updateData(uemp);
		} catch (Exception e) {
			e.printStackTrace();
			return "false" ;
		}
		return "ok" ;
	}
	
	public List<Uemp> getUempList(){//修改状态不需要加解密
		List<Uemp> list = super.findAll(" from Uemp where delstaff=0");
		return list ;
	}
	
	public LockState queryLockState(String date){
		List<LockState> list = super.findAll(" from LockState where  LOCKDATE='"+ date+"' ");
		if(list.size()>0){
			return list.get(0);
		}
		return null ;
	}
	
	public String saveOrUpdateOpenState(OpenState openState){
		try {
			wSHibernateDaoSupportProxy.saveOrUpdate(openState);
			//super.updateData(openState);
		} catch (Exception e) {
			e.printStackTrace();
			return "false";
		}
		return "success";
	}
	
	public OpenState getOpenState(String date){
		List<OpenState> list = super.findAll(" from OpenState where  OPENDATE='"+ date+"' ");
		if(list.size()>0){
			return list.get(0);
		}
		return null ;
	}
	
	public String insertLock(LockState lockState){
		
		try {
			super.addData(lockState);
		} catch (Exception e) {
			e.printStackTrace();
			return "false";
		}
		return "success";
	}
	
	public List<LockState> getLockState(String date){
		List<LockState> list = super.findAll(" from LockState where  LOCKDATE='"+ date+"' ");
		
		return list ;
	}
	
	public List<Checktemplet> getChecktempletListByDate(String date){
		List<Checktemplet> checktempletList = super
				.findAll(" from Checktemplet where  period='"+ date+"' ");
		
		
		if (checktempletList.size() > 0) {
			return checktempletList;
		} else {
			return null;
		}				
	}
	
	public List<Checktemplet> getChecktemplets(String staffint) {
		@SuppressWarnings("unchecked")
		List<Checktemplet> checktempletList = super
				.findAll(" from Checktemplet where  staffint='" + staffint
						+ "' ORDER BY period DESC");
		if (checktempletList.size() > 0) {
			return checktempletList;
		} else {
			return null;
		}
		//return checktempletList;

	}

	public Checktemplet getChecktemplet(String staffint) {

		@SuppressWarnings("unchecked")
		List<Checktemplet> checktempletList = super
				.findAll(" from Checktemplet where  staffint='" + staffint
						+ "' order by  did desc");

		if (checktempletList.size() > 0) {
			return checktempletList.get(0);
		} else {
			return null;
		}

	}
    /***
     * 查询薪资表信息
     * @param id
     * @return
     */
	public SalSalaryData getsalsalarydate(String id,String data){
	    List<SalSalaryData> date=super.findAll("from SalSalaryData where staffint='"+id+"' and period='"+data+"' and isDelete='"+SalConstant.NOT_DELETE+"' ");
	    List<SalSalaryData> list=new ArrayList<SalSalaryData>();
	    if(date.size()>0){
	    	  try {
	    			  list= CryptUtils.decryptNew(date);
	    			  
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    	 
	    	return list.get(0);
	    }else{
	    	return null;
	    }
		 
	}
	
	/**
	 * 未删除的用户
	 * @param staffint
	 * @return
	 */
	public Uemp getUemp(String staffint) {
		
		try {
			List<Uemp> uempList = super.findAll(" from Uemp where staffint='"
					+ staffint + "' and delstaff=0");
			//解密
			List<Uemp> list = CryptUtils.decryptNew(uempList);
			if (list.size() > 0) {
				
				return list.get(0);
			} else {

				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

	public Jbsalary findjbsalary(String id) {
		@SuppressWarnings("unchecked")
		List<Jbsalary> y = super
				.findAll(" from Jbsalary as s where  s.delstaff=0 and s.staffint='"
						+ id + "'");
		List<Jbsalary> list = null;
		try {
			//解密
			 list = CryptUtils.decryptNew(y);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (list.size() > 0) {
			return list.get(0);
		} else {
			return y.get(0);
		}
	}

	public Jbsalary findjbsalarying(String id) {//1表示已删除的记录
		@SuppressWarnings("unchecked")
		List<Jbsalary> y = super
				.findAll(" from Jbsalary as s where  s.delstaff=1 and  s.staffint='"
						+ id + "'  order by did desc ");
		List<Jbsalary> list = null;
		try {
			//解密
			 list = CryptUtils.decryptNew(y);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		if (list.size() > 0) {
			return list.get(0);
		} else {
			return y.get(0);
		}

	}

	public Integer getOutforworkint(String yearmouth) {
		@SuppressWarnings("unchecked")
		List<CheckDate> li = super
				.findAll("from CheckDate a  where a.yearmonth='" + yearmouth
						+ "' and iswork=1");
		Integer det = li.size();
		return det;

	}
	public Integer getNewmoth(String yearmouth) {
		@SuppressWarnings("unchecked")
		List<CheckDate> li = super
				.findAll("from CheckDate a  where a.yearmonth='" + yearmouth
						+ "'");
		Integer det = li.size();
		return det;

	}

	public Integer getOutwradint(String yearmouth) {
		@SuppressWarnings("unchecked")
		List<CheckDate> li = super
				.findAll("from CheckDate a  where a.yearmonth='" + yearmouth
						+ "'");
		Integer det = li.size();
		return det;

	}
	public Integer getOutforworkintNow(String yearMonth, String day) {

		List<CheckDate> li = super.findAll("from CheckDate  where yearmonth='"
				+ yearMonth + "' and  to_number(outforworkint)>='" + day + "'  and iswork=1");
		Integer det = 0 ;
		if(li!=null){
			 det = li.size();
		}	
		return det;
	}
	public Integer getforworad(String yearMonth, String day) {

		List<CheckDate> li = super.findAll("from CheckDate  where yearmonth='"
				+ yearMonth + "' and  to_number(outforworkint)>='" + day + "'");
		Integer det = 0 ;
		if(li!=null){
			 det = li.size();
		}
			
		return det;
			
	}
	public Integer getforworadtoo(String yearMonth, String day) {

		List<CheckDate> li = super.findAll("from CheckDate  where yearmonth='"
				+ yearMonth + "' and  to_number(outforworkint)>='" + day + "'");
		Integer det = 0 ;
		if(li!=null){
			 det = li.size();
		}
			
		return det;
			
	}
	public Integer getOutforworkLZ(String yearMonth, String day){
		List<CheckDate> li = super.findAll("from CheckDate  where yearmonth='"
				+ yearMonth + "' and  to_number(outforworkint)<='" + day + "'  and iswork=1");
		Integer det = 0 ;
		if(li!=null){
			 det = li.size();
		}
			
		return det;
	}
	public Integer getOutForm(String yearMonth, String day){
		List<CheckDate> li = super.findAll("from CheckDate  where yearmonth='"
				+ yearMonth + "' and  to_number(outforworkint)<='" + day + "'");
		Integer det = 0 ;
		if(li!=null){
			 det = li.size();
		}
			
		return det;
	}
	
	public List<CheckDate> getlist(String date, String dates) {
		String hql = "from CheckDate as c where  c.workdate between to_date('"
				+ date + "','yyyy/MM/dd') and to_date('" + dates
				+ "','yyyy/MM/dd')  and iswork=1";
		List<CheckDate> list = findAll(hql);
		return list;
	}

	public List<CheckDate> getfinddate(String max) {
		String hql = "from CheckDate as c where c.yearmonth='" + max
				+ "'  and iswork=1 order by workdate desc ";
		List<CheckDate> list = findAll(hql);
		return list;
	}

	
	
	public List<Jbsalary> getJbsalaryList(String staffint){
		String hql ="from Jbsalary where staffint = '"+ staffint +"'  and delstaff=0 order by did desc ";
		List<Jbsalary> list = findAll(hql);
		
		try {
			//解密
			List<Jbsalary> listJB =  CryptUtils.decrypt(list);
			return listJB ;
		} catch (Exception e) {
			e.printStackTrace();
			return null ;
		}
		
	}
	
	public String insertJB(Jbsalary jbsalary){
		try {
			//加密
			Jbsalary jb = CryptUtils.encrypt(jbsalary);
			super.addData(jb);
		} catch (Exception e) {
			e.printStackTrace();
			return "false" ;
		}
		return "ok";
	}
	
	public String updateJB(Jbsalary jbsalary){
		try {
			//加密
			Jbsalary jb = CryptUtils.encrypt(jbsalary);
			super.updateData(jb);
		} catch (Exception e) {
			e.printStackTrace();
			return "false" ;
		}
		return "ok" ;
	}
}
